from datetime import datetime
from sqlalchemy import Column, Integer, String, DECIMAL
from app.models.base import db, Base
import time


class SentenceWord(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_number = Column(String(30), nullable=False)
    sentence_index = Column(Integer)
    path = Column(String(1000), nullable=False)
    content = Column(String(2000), nullable=False)
    is_similar = Column(Integer, nullable=False, default=0)
    rate = Column(DECIMAL(5, 2), default=0)
    suggest = Column(String(5000), default='')
    check = Column(Integer, default=0)
    check_content = Column(String(2000), default='')
    check_id = Column(Integer, default=0)

    def keys(self):
        return ['id', 'order_number', 'sentence_index', 'path', 'content', 'is_similar', 'rate', 'suggest']

    def insert_sentences(self, sentences, order_number):
        current_check_content = ''
        params = []
        index = 0
        check_index = 0
        now = int(datetime.now().timestamp())
        for item in sentences:
            if item['check'] == 1 and item['check_content'] != current_check_content and item['content'] in item[
                'check_content']:
                current_check_content = item['check_content']
                check_index += 1
            params.append(
                {
                    'create_time': now,
                    'status': 1,
                    'order_number': order_number,
                    'sentence_index': index,
                    'content': item['content'],
                    'path': item['node'],
                    'is_similar': 0,
                    'check_content': item['check_content'],
                    'check_id': check_index,
                    'check': item['check']
                }
            )
            index += 1
        db.session.bulk_insert_mappings(SentenceWord, params)
        db.session.commit()
